package cn.xttc.edu.controller;


import cn.xttc.edu.entity.Customer;
import cn.xttc.edu.entity.ReturnMsg;
import cn.xttc.edu.service.CustomerService;
import cn.xttc.edu.util.MyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨磊
 * @since 2022-03-09
 */
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 按条件获取客户
     * @param pn     页码
     * @param limit  每页条数
     * @param search 搜索条件
     * @param sort 排序列名
     * @param order 排序方式
     * @return json字符串
     */
    @GetMapping("/customer")
    public String getAll(@RequestParam(value = "page") Integer pn,
                         @RequestParam("limit") Integer limit,
                         @RequestParam(value = "search", required = false, defaultValue = "") String search,
                         @RequestParam("sort") String sort,
                         @RequestParam("order") String order
    ) {
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<Customer>().like("name", search).or().like("phone", search);
        if ("asc".equalsIgnoreCase(order)) {
            customerQueryWrapper.orderByAsc(sort);
        } else if ("desc".equalsIgnoreCase(order)) {
            customerQueryWrapper.orderByDesc(sort);
        }
        Page<Customer> page = customerService.page(new Page<>(pn, limit), customerQueryWrapper);
        return MyUtil.formatReturnData(page);
    }

    /**
     * 获取用户总数
     *
     * @return 包装对象
     */
    @GetMapping("/customer/totalNum")
    public ReturnMsg getTotalNum() {
        long count = customerService.count();
        return ReturnMsg.success(count);
    }

    /**
     * 编辑客户信息
     *
     * @param customer 客户对象
     * @return 包装对象
     */
    @PutMapping("/customer")
    public ReturnMsg editCustomer(Customer customer) {
        try {
            customerService.updateById(customer);
            return ReturnMsg.success("编辑成功");
        } catch (Exception e) {
            return ReturnMsg.fail("编辑失败");
        }
    }

    /**
     * 单个删除和批量删除
     *
     * @param ids id字符串
     * @return 封装对象
     */
    @DeleteMapping("/customer/{ids}")
    public ReturnMsg deleteCars(@PathVariable("ids") String ids) {
        if (ids.contains("-")) {
            //批量删除
            List<Integer> delIds = new ArrayList<>();
            String[] strIds = ids.split("-");
            //组装id的集合
            for (String string : strIds) {
                delIds.add(Integer.parseInt(string));
            }
            try {
                customerService.removeBatchByIds(delIds);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败，该用户已与订单绑定");
            }
        } else {
            //单个删除
            Integer id = Integer.parseInt(ids);
            try {
                customerService.removeById(id);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败，该用户已与订单绑定");
            }
        }
        return ReturnMsg.success("删除成功");
    }
}

